<?php

class wwy_db
{
    protected $_fetch_mode = PDO::FETCH_ASSOC;
    
    /**
     * @var PDO
     */    
    protected $_connection = null;

    public function __construct($options)
    {
        $this->_set_connection($options);
    }

    protected function _set_connection($options)
    {
        $dsn = $options['dsn'];
        $user = $options['user'];
        $password = $options['password'];
        $this->_connection = new PDO($dsn, $user, $password);

        $this->_connection->exec("SET NAMES 'utf8'");
    }
    
    public function get_connection()
    {
        return $this->_connection;
    }
    
//     public function is_connected()
//     {
//         return ((bool) ($this->_connection instanceof PDO));
//     }
    
//     public function close_connection()
//     {
//         $this->_connection = null;
//     }

    public function query($sql, $bind = array())
    {    
        if (!is_array($bind)) {
            $bind = array($bind);
        }
         
        $stmt = $this->get_connection()->prepare($sql);
        $stmt->execute($bind);    //by gbfun!! 【执行$sql】
    
        $stmt->setFetchMode($this->_fetch_mode);
    
        return $stmt;
    }
    
    public function last_insert_id()
    {         
        $last_insert_id = $this->get_connection()->lastInsertId();
    
        return $last_insert_id;
    }
    
    public function fetch_all($sql, $bind = array(), $fetch_mode = null)
    {
        if ($fetch_mode === null) {
            $fetch_mode = $this->_fetch_mode;
        }
    
        $stmt = $this->query($sql, $bind);    //@by gbfun 里面已经【$stmt->execute($bind)】
        $result = $stmt->fetchAll($fetch_mode);
    
        return $result;
    }
    
    public function fetch_row($sql, $bind = array(), $fetch_mode = null)
    {
        if ($fetch_mode === null) {
            $fetch_mode = $this->_fetch_mode;
        }
    
        $stmt = $this->query($sql, $bind);
        $result = $stmt->fetch($fetch_mode);    //【fetch()】
    
        return $result;
    }
    
    public function fetch_assoc($sql, $bind = array())
    {
        $stmt = $this->query($sql, $bind);
    
        $data = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $tmp = array_values(array_slice($row, 0, 1));   //以【第一个】字段的【值】为索引
            $data[$tmp[0]] = $row;
        }
    
        return $data;
    }
    
    public function fetch_col($sql, $bind = array())
    {
        $stmt = $this->query($sql, $bind);
        $result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);    //只获取一列，直接存储每一行的值； 而不会以【列名】【数字】作为【列值】的索引
    
        return $result;
    }
    
    public function fetch_pairs($sql, $bind = array())
    {
        $stmt = $this->query($sql, $bind);
    
        $data = array();
        while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
            $data[$row[0]] = $row[1];
        }
    
        return $data;
    }
    
    public function fetch_one($sql, $bind = array())
    {
        $stmt = $this->query($sql, $bind);
        $result = $stmt->fetchColumn(0);    //@获取下一行的一列，直接第一行的值； 而不会以【列名】【数字】作为【列值】的索引
    
        return $result;
    }
    
    protected function _quote($value, $type = null)
    {
        return $this->get_connection()->quote($value);
    }
    
    public function quote($value, $type = null)
    {    
        if (is_array($value)) {
            foreach ($value as &$val) {
                $val = $this->quote($val, $type);
            }
            return implode(', ', $value);
        }
    
        return $this->_quote($value, $type);
    }
    
    public function limit($sql, $count, $offset = 0)
    {
        $count  = intval($count);
        $offset = intval($offset);
    
        $sql .= " LIMIT $count";
        if ($offset > 0) {
            $sql .= " OFFSET $offset";
        }
    
        return $sql;
    }
}
